package club.emergency.wechat.department_staff.search.consumer.controller;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.department_staff.search.consumer.service.DepartmentStaffSearchConsumer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.department_staff.search.consumer.controller
 * Date: 2019/4/2
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api("企业人员搜索接口")
@RestController
@RequestMapping("/department-staff-search")
public class DepartmentStaffSearchController {

    private DepartmentStaffSearchConsumer departmentStaffSearchConsumer;

    public DepartmentStaffSearchController(DepartmentStaffSearchConsumer departmentStaffSearchConsumer) {
        this.departmentStaffSearchConsumer = departmentStaffSearchConsumer;
    }

    @ApiOperation("查询企业人员接口,两个参数非必传，单同时只传一个值。departmentId传的时候指向性查询该id下的所有人， departmentCode传的时候查询该code及其以下的所有人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "企业id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "departmentCode", value = "企业匹配字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "当前页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示的数据数量", dataType = "int", paramType = "query")
    })
    @PostMapping("/flipList")
    public ResponseEntity filpList(
            @RequestParam(value = "departmentId", required = false) Long departmentId,
            @RequestParam(value = "departmentCode", required = false) String departmentCode,
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(departmentStaffSearchConsumer.flipList(departmentId, departmentCode, pageNo, pageSize), ResponseEntity.class);
    }

    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "微信openId", name = "openId", dataType = "string", paramType = "query"),
    })
    @PostMapping("/signature")
    public ResponseEntity signature(
            HttpServletResponse httpServletResponse,
            @RequestParam("openId") String openId
    ) {
        String signature = departmentStaffSearchConsumer.signature(openId);
        ResponseEntity responseEntity = new JsonConvert<ResponseEntity>().jsonDecode(signature, ResponseEntity.class);
        if (responseEntity.getStatusCode() != StatusCode.LOGIN_OK) {
            return responseEntity;
        }
        //把数据放在authorization中
        httpServletResponse.setHeader("Authorization", new JsonConvert<>().jsonEncode(responseEntity.getData()));
        return responseEntity;
    }

    @ApiOperation("更新Token")
    @PostMapping("/updateToken")
    public ResponseEntity updateToken(HttpServletResponse httpServletResponse) {
        String updateToken = departmentStaffSearchConsumer.updateToken();
        ResponseEntity responseEntity = new JsonConvert<ResponseEntity>().jsonDecode(updateToken, ResponseEntity.class);
        if (responseEntity.getStatusCode() != StatusCode.LOGIN_OK) {
            return responseEntity;
        }
        //把数据放在authorization中
        httpServletResponse.setHeader("Authorization", new JsonConvert<>().jsonEncode(responseEntity.getData()));
        return responseEntity;
    }
}
